package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Biao;
import com.hopu.pojo.Emp;
import com.hopu.pojo.Mybs;
import com.hopu.service.BiaoService;
import com.hopu.service.BsService;
import com.hopu.util.DateFormatGetTime;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/bs")
@RestController
public class BsController {
    @Resource
    private BsService bsServiceImpl;

    @PostMapping("/findAllBs")
    private ResultUtils findAllBs(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Mybs> allBs = bsServiceImpl.findAllBs();

            PageInfo pageInfo = new PageInfo(allBs);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("/addBs")
    public ResultUtils addBs(
            HttpSession session,
            @RequestParam(value = "hid")int hid,
            @RequestParam(value = "bremark")String bremark
            ){
        try {

            Emp empLog = (Emp) session.getAttribute("empLog");

            int num = bsServiceImpl.addBs(hid,new Date().getTime(),bremark,empLog.getEid());

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/findBsByBid")
    public ResultUtils findBsByBid(int bid){

        Mybs mybs = bsServiceImpl.findBsByBid(bid);

        return new ResultUtils(200,"查询页面成功",mybs);

    }
    @PostMapping("/uppBs")
    public ResultUtils uppBs(Mybs mybs, HttpSession session){

            // 获取session 当前登录账号信息
            Emp empLog = (Emp)session.getAttribute("empLog");
            mybs.setEid(empLog.getEid());

            mybs.setMtime(new Date().getTime());


            int biao1 = bsServiceImpl.uppBs(mybs);
            if (biao1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);


    }
}
